#modifyId200712202327
#modify to show more information


use strict;
my $line;
my $numargs = @ARGV;
if($numargs < 4)
{
	die("ExSingleInfo.pl ReportFileName SiteNumber MutaionTypes[1457] outpath\n\n");
}

my $infile = $ARGV[0]; #iput file 
my $site = $ARGV[1]; # site number
my $mutType = $ARGV[2];#mutation type
my $outPath = $ARGV[3];#out put path
#modifyId200712202327
my $parslt = $infile;
$parslt =~s/report\.txt/parseRlt\.txt/;
my $mirRlt = $infile;
$mirRlt =~s/report\.txt/mirandaRlt\.txt/;


print $parslt." ".$mirRlt;

unless(open(INFILE, $infile))
{
	die("Can not open $infile!/n/n");
}

$outPath.=$site.".html";
unless(open(OUTFILE, ">$outPath"))
{
	die("can not create $site.html file");
}
###create header
print OUTFILE '
<html>
<header>
<title>Site '.$site.'</title>
</headr>
<body>
';
#####
unless(open(MIRRLT,$mirRlt))
{
	print OUTFILE "Can $ARGV[0] not $infile  cccccccc open $mirRlt";
}
my $mirRltPath = "";
while($line =<MIRRLT>)
{
	if($line=~/Query Filename\:/)
	{
		chomp($line);
		my @field = split(/\t+/, $line);
		$field[1] =~s/hsa-mature-miR\.fasta/miRBASE\.txt/;
		$mirRltPath = $field[1];
		#print OUTFILE $field[2];
		last;
	}
}
close();
###########
my $count =0;
my @list = split(//, $mutType);
my $microRNA = "";
my $oldmiR = "";
my $mirbaseUrl = "http://microrna.sanger.ac.uk/cgi-bin/sequences/mirna_entry.pl?acc=";
while($line = <INFILE>)
{
	chomp($line);
	
	if($line=~/^>/)
	{	
	
	my @field = split(/ /, $line);
	$microRNA = $field[1];
	next;
	}
	
	my @mysplit = split(/[#\|]/, $line);
	my $thisType = $mysplit[9];
	my $id = $mysplit[0];
	
	if($id == $site)
	{
		my $valid = -1;
		for(my $i=0; $i<=$#list; $i++)
		{
			if($thisType == $list[$i])
			{
				$valid = $thisType;
				last;
			}
		}
		
		if($valid > 0)
		{
		
			if($oldmiR !~/$microRNA/)
			{
				&getMirbaseInfo($microRNA, $mirbaseUrl, $mirRltPath);
				
				$oldmiR = $microRNA;
								
				&getOriginInfo($parslt, $microRNA);
			}
			
			print OUTFILE $line."  <a href= \"\./help.html#$valid\">?</a>"."</p>";
		}
	}
}

####CREATE END OF FILE
print OUTFILE '
</body>
</html>
';
close($infile);
#modifyId200712202327
sub getOriginInfo()
{
	my ($resultfile, $curMiR) = @_;
	unless(open(PARSERESULT, $resultfile))
	{
		die("Can not open $resultfile !/n/n");
	}

	my $line = "";
	while($line=<PARSERESULT>)
	{
		if($line=~/$curMiR/)
		{
			while($line=<PARSERESULT>)
			{	
			
				if($line=~/TargetEnd/)
				{

					last;
				}
				else
				{
					chomp($line);
					print OUTFILE $line."</p>";
				}
			}
			last;
		}
	}
	print OUTFILE "<strong>Mutant</strong></p>";
	
	close(PARSERESULT);
}

sub getMirbaseInfo()
{
	my($miR, $url, $mirbase) = @_;
	unless(open(MIRBASE, $mirbase))
	{
		print OUTFILE  "can not open $mirbase\n";
		print OUTFILE "<hr><b><font size=5>".$microRNA."</font></b></p><strong>Wild</strong></p>";
		return;
	}
	my $line = ""; 
	print OUTFILE "<hr><b><font size=5>$microRNA</font></b>";
	while($line = <MIRBASE>)
	{
		
		if($line=~/$miR/i)
		{
			chomp($line);
			my @field = split(/\s+/, $line);
			print OUTFILE "   <a href= $url$field[0]>$field[0]</a>";
		}
	}
	print OUTFILE "</p><strong>Wild</strong></p>";
	close(MIRBASE);
}